﻿using System.Text;

namespace Sharp.Portal.AdminLTE.FormField
{
    /// <summary>
    /// 选取框字段
    /// </summary>
    public class SelectBoxField : ElementField
    {
        /// <summary>
        /// 选取框字段
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="dataSourceName"></param>
        /// <param name="displayField"></param>
        /// <param name="valueField"></param>
        public SelectBoxField(string fieldName, string dataSourceName, string displayField, string valueField)
        {
            this.FieldName = fieldName;
            this.WindowUrl = dataSourceName;
            this.DisplayField = displayField;
            this.ValueField = valueField;
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return $"@Html.Bootstrap().LableFor(m=>m.{this.FieldName})" + base.Build(nonFirst);
            }

            var windowUrlParm = $"?selectTextControl={FieldName}";
            var sb = new StringBuilder();


            sb.AppendFormat("@Html.Bootstrap().SelectBoxFor(m => m.{0},\"{2}\").WindowUrl(\"{1}\").ValueField(\"{2}\")", FieldName, WindowUrl + windowUrlParm, ValueField);


            sb.AppendFormat("\r\n<script type = \"text/javascript\">\r\nvar {1}List = new app.SelectBox();\r\n{1}List.init(\"{1}\", \"@Model.{0}\");\r\n</script>", FieldName, FieldName.Replace(".", "_"));

            sb.AppendFormat("@Html.ValidationMessageFor(m => m.{0})", FieldName);

            return sb.ToString();
        }
        /// <summary>
        /// 值字段
        /// </summary>
        public object ValueField { get; set; }
        /// <summary>
        /// 显示字段
        /// </summary>
        public object DisplayField { get; set; }
        /// <summary>
        /// WindowUrl
        /// </summary>
        public string WindowUrl { get; set; }
    }
}
